/**
 * 工资卡绑定
 * @author  Jello
 * @create  2018/10/18 10:45
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var sessionStorage = window.sessionStorage;
var spinner = new Spinner(opts);
var type = 1;
var userid_sign = "";

$(function() {

	userid_sign = sessionStorage.getItem("userid_sign");
	if(!userid_sign) {
		userid_sign = sessionStorage.getItem("openid");
	}

	//检测驻场是否填写入职者姓名和身份证号
	var userName = sessionStorage.getItem("bank_user_name"); //用户真实姓名
	var identityNo = sessionStorage.getItem("identity_no"); //身份证号
	if(userName != "" && userName != null) {
		$("#us_name").val(userName);
	}
	if(identityNo != "" && identityNo != null) {
		var decrypt = new JSEncrypt();
		decrypt.setPrivateKey(privateKey);　
		$("#us_certNo").val(decrypt.decrypt(identityNo));;
	}
	var card_no = sessionStorage.getItem("card_no");
	if(card_no == "" || card_no == null) {
		if((identityNo != "" && identityNo != null) || (userName != "" && userName != null)) {
			type = 2;
		}
	}

	//绑定工资卡
	$("#bindcard_btn").click(function() {
		backLogin(userid_sign, "../../login.html", function() {
			var userName = $("#us_name").val(); //姓名
			var certNo = $("#us_certNo").val(); //身份证号
			var cardNo = $("#us_cardNo").val(); //银行卡号
			var reservedPhone = $("#us_reservedPhone").val(); //预留手机号
			var withdrawPwd = $("#us_withdrawPwd").val(); //提现密码
			var flag = checkBankCard(userName, certNo, cardNo, reservedPhone, withdrawPwd, 1);
			if(flag) {
				var encrypt = new JSEncrypt();
				encrypt.setPublicKey(privateKey);　
				certNo = encrypt.encrypt(certNo);
				cardNo = encrypt.encrypt(cardNo);

				//绑定工资卡接口上送
				$.ajax({
					type: "post",
					url: serverUrl + "/userbank/bindunbind_bankcard?sgin=" + userid_sign,
					data: "{\"userName\":\"" + userName + "\",\"identityNo\":\"" + certNo + "\",\"bankNumber\":\"" + cardNo + "\",\"bankUserTel\":\"" + reservedPhone + "\",\"moneyPassword\":\"" + hex_md5(hex_md5(withdrawPwd) + salt) + "\",\"type\":\"" + type + "\"}",
					cache: false,
					async: false,
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					crossDomain: true,
					timeout: 15000,
					beforeSend: function() {
						$("#myspin").text("");
						var target = $("#myspin").get(0);
						spinner.spin(target);
					},
					success: function(data) {
						spinner.spin();
						var resultObj = eval(data);
						if(resultObj.code == 0 && resultObj.data != null) {
							if(resultObj.data.res_code == 0) {
								window.location.href = "paycardbinding_succ.html";
							} else {
								$("#errNum").html(resultObj.data.res_error_num);
								$("#errMsg").html(resultObj.data.res_msg);
								$(".esc_box").show();
								$(".esc_divbox").show();
							}
						} else if(resultObj.code == 20029) {
							$(".esc_box2").show();
							$(".esc_divbox2").show();
						} else {
							$.myToast(resultObj.msg);
							return;
						}
					},
					error: function(XMLHttpRequest, status, errorThrown) {
						spinner.spin();
						if(status == 'timeout') {
							$.myToast("请求超时");
						} else {
							$.myToast("请求网络服务数据失败");
						}
						return;
					}
				});
			}
		})
	})

	//错误次数提示框
	$("#tm_ckdeil").click(function() {
		$(".esc_box").hide();
		$(".esc_divbox").hide();
	})

	//绑定机会已使用完提示框
	$("#tm_ckdeil2").click(function() {
		$(".esc_box2").hide();
		$(".esc_divbox2").hide();
	})

})